package com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.suggestinfo.analysis.annualrisk.repository.entity.AnnualRiskEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.disasterscenario.repository.entity.DisasterScenarioEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.emergencydisaster.repository.entity.EmergencyDisasterEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.emergencydisposal.repository.entity.EmergencyDisposalEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.service.SuggestionAnalysisService;
import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.ParamsVo;
import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.SuggestionAnalysisParams;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/suggestionAnalysis")
@Slf4j
public class SuggestionAnalysisController {

    @Autowired
    SuggestionAnalysisService suggestionAnalysisService;

    /**
     * 获取任务列表
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/queryTaskList")
    public RestResponse queryDisasterData(HttpServletRequest request, SuggestionAnalysisParams queryParams) {
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> map = suggestionAnalysisService.queryTaskList(queryParams,curPage,pageSize);
            response = RestResponse.succeed(map);
            response.setMessage("获取数据成功");
        }catch (Exception e){
            log.error("获取数据失败!",e);
            response=RestResponse.fail("获取数据失败!");
        }
        return response;
    }

    /**
     * 根据任务地区获取区县名称  市名称
     * @param   taskDistrict
     * @param   flag(0:按区县,1:按地市)
     * @return
     */
    @GetMapping("/getDivisionByTaskDistrict")
    public RestResponse getByDistrict(String taskNum, String taskDistrict, String suggestionName, String flag) {
        RestResponse response = null;
        try{
            Map<String, Object> map = suggestionAnalysisService.getDivisionByTaskDistrict(taskNum,taskDistrict, suggestionName, flag);
            response = RestResponse.succeed(map);
            response.setMessage("获取数据成功");
        }catch (Exception e){
            log.error("获取数据失败!",e);
            response=RestResponse.fail("获取数据失败!");
        }
        return response;
    }

    /**
     * 保存灾害情景分析
     * @param list
     * @return
     */
    @PostMapping("/saveDisasterScenario")
    public RestResponse saveDisasterScenario(@RequestBody List<DisasterScenarioEntity> list) {
        RestResponse response = null;
        try{
            Boolean res = suggestionAnalysisService.saveDisasterScenario(list);
            if(res){
                response = RestResponse.succeed("保存灾害情景分析成功");
                response.setMessage("保存灾害情景分析成功");
            }else{
                response=RestResponse.fail("保存灾害情景分析失败!");
            }
        }catch (Exception e){
            log.error("保存灾害情景分析失败!",e);
            response=RestResponse.fail("保存灾害情景分析失败!");
        }
        return response;
    }

    /**
     * 获取灾害情景分析详情页(对策建议分析模块)
     * @param params
     * @return
     */
    @GetMapping("/getDisasterScenarioData")
    public RestResponse getDisasterScenarioData(ParamsVo params) {
        RestResponse response = null;
        try{
            DisasterScenarioEntity disasterScenarioEntity = suggestionAnalysisService.getDisasterScenarioData(params);
            response = RestResponse.succeed(disasterScenarioEntity);
            response.setMessage("获取详情页数据成功");
        }catch (Exception e){
            log.error("获取详情页数据失败!",e);
            response=RestResponse.fail("获取详情页数据失败!");
        }
        return response;
    }

    /**
     * 保存年度风险应对策略建议
     * @param list
     * @return
     */
    @PostMapping("/saveAnnualRisk")
    public RestResponse saveAnnualRisk(@RequestBody List<AnnualRiskEntity> list) {
        RestResponse response = null;
        try{
            Boolean res = suggestionAnalysisService.saveAnnualRisk(list);
            if(res){
                response = RestResponse.succeed("保存年度风险应对策略建议成功");
                response.setMessage("保存年度风险应对策略建议成功");
            }else{
                response=RestResponse.fail("保存年度风险应对策略建议失败!");
            }
        }catch (Exception e){
            log.error("保存年度风险应对策略建议!",e);
            response=RestResponse.fail("保存年度风险应对策略建议!");
        }
        return response;
    }

    /**
     * 获取年度风险应对策略建议详情页(对策建议分析模块)
     * @param params
     * @return
     */
    @GetMapping("/getAnnualRiskData")
    public RestResponse getAnnualRiskData(ParamsVo params) {
        RestResponse response = null;
        try{
            AnnualRiskEntity annualRiskEntity = suggestionAnalysisService.getAnnualRiskData(params);
            response = RestResponse.succeed(annualRiskEntity);
            response.setMessage("获取详情页数据成功");
        }catch (Exception e){
            log.error("获取详情页数据失败!",e);
            response=RestResponse.fail("获取详情页数据失败!");
        }
        return response;
    }

    /**
     * 保存/修改 应急备灾对策
     * @param list
     * @return
     */
    @PostMapping("/saveEmergencyDisaster")
    public RestResponse saveEmergencyDisaster(@RequestBody List<EmergencyDisasterEntity> list) {
        RestResponse response = null;
        try{
            Boolean res = suggestionAnalysisService.saveEmergencyDisaster(list);
            if(res){
                response = RestResponse.succeed("保存应急备灾对策成功");
                response.setMessage("保存应急备灾对策成功");
            }else{
                response=RestResponse.fail("保存应急备灾对策失败!");
            }
        }catch (Exception e){
            log.error("保存应急备灾对策失败!",e);
            response=RestResponse.fail("保存应急备灾对策失败!");
        }
        return response;
    }

    /**
     * 获取应急备灾对策详情页(对策建议分析模块)
     * @param params
     * @return
     */
    @GetMapping("/getEmergencyDisasterData")
    public RestResponse getEmergencyDisasterData(ParamsVo params) {
        RestResponse response = null;
        try{
            EmergencyDisasterEntity emergencyDisasterEntity = suggestionAnalysisService.getEmergencyDisasterData(params);
            response = RestResponse.succeed(emergencyDisasterEntity);
            response.setMessage("获取详情页数据成功");
        }catch (Exception e){
            log.error("获取详情页数据失败!",e);
            response=RestResponse.fail("获取详情页数据失败!");
        }
        return response;
    }

    /**
     * 保存应急处置对策
     * @param list
     * @return
     */
    @PostMapping("/saveEmergencyDisposal")
    public RestResponse saveEmergencyDisposal(@RequestBody List<EmergencyDisposalEntity> list) {
        RestResponse response = null;
        try{
            Boolean res = suggestionAnalysisService.saveEmergencyDisposal(list);
            if(res){
                response = RestResponse.succeed("保存应急处置对策成功");
                response.setMessage("保存应急处置对策成功");
            }else{
                response=RestResponse.fail("保存应急处置对策失败!");
            }
        }catch (Exception e){
            log.error("保存应急处置对策失败!",e);
            response=RestResponse.fail("保存应急处置对策失败!");
        }
        return response;
    }

    /**
     * 获取应急处置对策详情页(对策建议分析模块)
     * @param params
     * @return
     */
    @GetMapping("/getEmergencyDisposalData")
    public RestResponse getEmergencyDisposalData(ParamsVo params) {
        RestResponse response = null;
        try{
            EmergencyDisposalEntity emergencyDisasterEntity = suggestionAnalysisService.getEmergencyDisposalData(params);
            response = RestResponse.succeed(emergencyDisasterEntity);
            response.setMessage("获取详情页数据成功");
        }catch (Exception e){
            log.error("获取详情页数据失败!",e);
            response=RestResponse.fail("获取详情页数据失败!");
        }
        return response;
    }

    /**
     * 根据任务编号获取 灾害情景分析   应急备灾对策  应急处置对策  的市级数据
     * @param taskNum
     * @return
     */
    @GetMapping("/getDataByTaskNum")
    public RestResponse getDataByTaskNum(String taskNum) {
        RestResponse response = null;
        try{
            Map<String,Object> map = suggestionAnalysisService.getDataByTaskNum(taskNum);
            response = RestResponse.succeed(map);
            response.setMessage("获取数据成功");
        }catch (Exception e){
            log.error("获取数据失败!",e);
            response=RestResponse.fail("获取数据失败!");
        }
        return response;
    }

}
